<template>
  <div ref="imageWrapper">
    <div class="modelTitle"><h2>分包管理模型</h2></div>
    <a-card title="查询区域" class="search">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form-model ref="ruleForm" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="onSearch">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="审计单位" prop="unitName">
                <a-input v-model="queryParam.unitName" readOnly placeholder="请选择" @click.native="openOrganModel" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="6">
              <a-form-model-item label="任期时间" prop="arrayTime">
                <a-range-picker
                  style="width:100%"
                  v-model="queryParam.arrayTime"
                  format="yyyy-MM-DD"
                  value-format="yyyy-MM-DD"
                  @change="handleDatechange"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" icon="search" class="yellow_search_btn" @click="onSearch">查询</a-button>
                <a-button icon="reload" class="clear_btn" @click="searchReset">清除条件</a-button>
                <a-button
                  type="danger"
                  :loading="ydLoading"
                  @click="handleToYD()"
                  icon="arrow-right"
                  style="margin-left: 18px"
                >
                  转疑点
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card style="margin-top: 20px">
      <a-table
        :columns="columns"
        :data-source="dataSource"
        row-key="con_id"
        :bordered="true"
        :loading="loading"
        class="antableClass"
        :pagination="ipagination"
        :row-selection="rowSelection"
        @change="handleTableChange"
      >
        <template v-slot:unit_prc="text, record">
          <a href="javascript:;" @click="handlCheckDetail(record, 'unit_prc')">{{ text }}</a>
        </template>
        <template v-slot:last_ex_tax_amt="text, record">
          <a href="javascript:;" @click="handlCheckDetail(record, 'last_ex_tax_amt')">{{ text }}</a>
        </template>
        <template v-slot:guid_prc="text, record">
          <a href="javascript:;" @click="handlCheckDetail(record, 'guid_prc')">{{ text }}</a>
        </template>
        <template v-slot:eav_unit_prc="text, record">
          <a href="javascript:;" @click="handlCheckDetail(record, 'eav_unit_prc')">{{ text }}</a>
        </template>
      </a-table>
    </a-card>
    <AuditDepart ref="AuditDepart" @selectedRow="updateQueryParam" />
    <YDModel ref="YDModel" :router-data="$route.path" />
    <CheckDetail :columns="columns" ref="CheckDetail" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, putAction, postAction } from '@/api/manage'
import AuditDepart from '@/views/sj/sjmxgl/cwzb/components/AuditDepart'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/sjmxgl/gcxmgl/zrcbgl/YDModel'
import columns from './columns'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
import CheckDetail from './CheckDetail.vue'
export default {
  components: { AuditDepart, CheckDetail, YDModel },
  mixins: [JeecgListMixin, base64toImg, jumpConfigMixin],
  data() {
    return {
      queryParam: {
        unitName: '',
        orgId: '',
        unitId: '',
        startTime: '',
        endTime: '',
        arrayTime: ['', '']
      },
      superParam: {
        id: '477a6516c6e94b40959fcd2e7da62973',
        modelId: '477a6516c6e94b40959fcd2e7da62973',
        extraFlag: true
      },
      dataSource: [],
      isBackButton: false,
      url: {
        list: '/sjmx/querySubpackage',
        // 查询模型转疑点信息(多选)
        TurnSuspects: '/sjmx/queryModelMsgMulti'
      },
      rules: {
        unitName: [{ required: true, message: '请选择审计单位' }],
        arrayTime: [{ required: true, message: '请选择审计时间' }]
      },
      columns: columns(),
      disableMixinCreated: true
    }
  },
  watch: {
    $route: {
      handler: function(to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          let { unitName, orgId, startTime, endTime, arrayTime, selectedRowKeys } = to.query
          this.queryParam = {
            unitName,
            orgId,
            startTime,
            endTime,
            arrayTime: [startTime, endTime]
          }
          this.selectedRowKeys = [...selectedRowKeys.split(',')]
          console.log(this.selectedRowKeys)
          this.$nextTick(() => {
            this.onSearch()
          })
        }
      },
      immediate: true
    }
  },
  computed: {
    // 行选择
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = [...selectedRowKeys]
          this.selectedRows = [...selectedRows]
        }
      }
    }
  },
  filters: {
    TimerFormat(time) {
      return time.indexOf(' ') ? time.split(' ')[0] : time
    }
  },
  created() {},
  methods: {
    updateQueryParam(checkRow) {
      console.log(checkRow)
      this.queryParam.unitName = checkRow.departName
      this.queryParam.orgId = checkRow.id
    },
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.dataSource = []
    },
    handlCheckDetail(record, key) {
      this.$refs.CheckDetail.check(record, key)
    },
    onSearch() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loadData(1)
        }
      })
    },
    openOrganModel() {
      this.$refs.AuditDepart.show()
    },
    handleToYD() {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        // let ydnr = `中铁XX局XX项目未取得增值税劳务发票X亿元，设计税金X万元；X公司城际联络线`
        let param = Object.assign({}, this.queryParam, {
          selectedRowKeys: this.selectedRowKeys.join(',')
        })
        let ydnr = undefined
        let gzzd = undefined
        const time = `【${startTime}～${endTime}】【分包管理模型】`
        let ydList = JSON.stringify(this.selectedRows)
        postAction(this.url.TurnSuspects, Object.assign({}, this.queryParam, this.superParam, { ydList: ydList })).then(
          res => {
            if (res.success) {
              if (res.result.length) {
                ydnr = res.result[0].ydms ? res.result[0].ydms : ydnr
                gzzd = res.result[0].gzzd ? res.result[0].gzzd : gzzd
              }
              this.addYdMulti(param, ydnr, time, gzzd)
            } else {
              this.$message.error(res.message)
            }
            if (res.code === 510) {
              this.$message.warning(res.message)
            }
          }
        )
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    handleDatechange(dates) {
      this.queryParam.startTime = dates[0]
      this.queryParam.endTime = dates[1]
    }
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';

.modelTitle {
  display: flex;
  justify-content: center;
  align-items: center;
  h2 {
    font-weight: 600;
  }
}

.img_css /deep/ .ant-card-body {
  padding-top: 7px;
}

/deep/ .ant-table-thead {
  tr {
    th {
      text-align: center !important;
    }
  }
}
</style>
